-- 威慑技能
local LuaWeishe = fk.CreateSkill{
  name = "LuaWeishe",
  anim_type = "control",
  tags = { Skill.Permanent }
}

Fk:loadTranslationTable{
  ["LuaWeishe"] = "威慑",
  ["@weishe_turn_count"] = "威慑回合：",
  ["@weishe"] = "<font color='#5c5c58'>威慑值：</font>",
  ["@@yizhongyuanweimianshuai"] = "<font color='#f0ff00'>以众愿为冕</font>",
  [":LuaWeishe"] = "持恒技，每四个回合结束时，若你四个回合内均未受到过伤害，你获得“以众愿为冕”标记，若你的“权柄”已达到上限，你获得1点“威慑”标记，你的“威慑”标记上限为2；当你“威慑”标记达到2时，你获得【威临】若你已拥有【威临】视为未发动过。拥有“以众愿为冕”标记的角色造成雷属性伤害时，此伤害值+1。",
}
-- 记录受伤情况
LuaWeishe:addEffect(fk.DetermineDamageInflicted, {
    is_delay_effect = true,
    mute = true,
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(LuaWeishe.name)
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:setPlayerMark(player, "weishe_damaged-turn", 1)
    room:setPlayerMark(player, "@weishe_turn_count", 0)
  end,
})

-- 回合结束时检查
LuaWeishe:addEffect(fk.TurnEnd, {
    is_delay_effect = true,
    mute = true,
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(LuaWeishe.name)
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if player:getMark("weishe_damaged-turn") == 0 then
        room:addPlayerMark(player, "@weishe_turn_count", 1)
    else
        room:setPlayerMark(player, "@weishe_turn_count", 0)
    end
    
    local turn_count = player:getMark("@weishe_turn_count")
    -- 第四回合结束时检查
    if turn_count >= 4 then
        room:notifySkillInvoked(player, "LuaWeishe")
      room:setPlayerMark(player, "@weishe_turn_count", 0) -- 重置计数

        -- 获得"以众愿为冕"标记
        room:setPlayerMark(player, "@@yizhongyuanweimianshuai", 1)
        
        -- 检查权柄是否已满
        if player:getMark("@quanbing") >= 10 then
          -- 获得威慑标记
          local weishe = player:getMark("@weishe") or 0
          weishe = math.min(weishe + 1, 2)
          room:setPlayerMark(player, "@weishe", weishe)
          
          -- 检查威慑是否达到2点
          if weishe == 2 then
            -- 获得威临技能
            if player:hasSkill("LuaWeiling") then
              -- 如果已有威临，重置其使用状态
              player:setSkillUseHistory("LuaWeiling", 0, Player.HistoryGame)
            else
              -- 获得威临技能
              room:handleAddLoseSkills(player, "LuaWeiling", nil)
            end
            -- 清空威慑标记
            room:setPlayerMark(player, "@weishe", 0)
          end
        end

    end
  end,
})

-- 雷属性伤害加成
LuaWeishe:addEffect(fk.DamageCaused, {
    is_delay_effect = true,
    mute = true,
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(LuaWeishe.name) and target:getMark("@@yizhongyuanweimianshuai") > 0 and data.damageType == fk.ThunderDamage
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    data.damage = data.damage + 1
  end,
})

  return LuaWeishe
